package com.shfl.servermanage.logic.api;


import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.common.*;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.model.request.AddExecutableFileRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.SysExecutableFilePageResponse;
import com.shfl.servermanage.logic.service.SysExecutableFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/executable/file")
@Api(value = "执行文件", description = "执行文件")
public class ExecutableFileController {

    @Autowired
    private SysExecutableFileService sysExecutableFileService;

    @GetMapping(value = "/page/list")
    public ApiResultPagingHelper<SysExecutableFilePageResponse, Void> page(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        objectMap.put("deleteFlg", CommonConstants.DeleteFlg.NOT_DELETE.getCode());
        PageInfo<SysExecutableFilePageResponse> pageInfo = sysExecutableFileService.findPage(requestHelper.getPageable(), objectMap);
        if (pageInfo.getTotal() > 0) {
            pageInfo.getList().forEach(item -> {
                item.setExecutableTypeValue(CommonConstants.ExecutableType.getValue(item.getExecutableType()));
            });
        }
        return new ApiResultPagingHelper<>(SystemConstant.RE_SUCCESS_CODE,
                pageInfo.getTotal(), pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @GetMapping(value = "/list")
    public ApiResultListHelper<SysExecutableFilePageResponse> list(
            RequestHelper requestHelper
    ) {
        requestHelper.apply();
        Map<String, Object> objectMap = requestHelper.getKeyMap();
        objectMap.put("deleteFlg", CommonConstants.DeleteFlg.NOT_DELETE.getCode());
        List<SysExecutableFilePageResponse> list = sysExecutableFileService.findList(objectMap);
        return new ApiResultListHelper<>(SystemConstant.RE_SUCCESS_CODE, list);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增执行文件", notes = "新增执行文件")
    public ApiResultHelper add(
            @RequestBody AddExecutableFileRequest addExecutableFileRequest
    ) throws Exception {
        sysExecutableFileService.add(addExecutableFileRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

    @PostMapping(value = "/remove")
    @ApiOperation(value = "移除执行文件", notes = "移除执行文件")
    public ApiResultHelper remove(
            @RequestBody RemoveByIdRequest removeByIdRequest
    ) throws Exception {
        sysExecutableFileService.remove(removeByIdRequest);
        return new ApiResultHelper(SystemConstant.RE_SUCCESS_CODE);
    }

}
